package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.common.utils.FileTypeUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.ProductionPlanMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProductionPlan;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProductionPlanVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.ProductionPlanService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyi on 2016/11/18.
 * 生产计划 业务层
 */
public class ProductionPlanServiceImpl implements ProductionPlanService {
    @Autowired
    public ProductionPlanMapper productionPlanMapper;
    @Autowired
    private TdmFileMapper tdmFileMapper;

    /**
     * 查看生产计划列表信息
     *
     *
     * @param employeeVo
     * @param productionPlan 生产计划信息
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> list(EmployeeVo employeeVo, ProductionPlanVo productionPlan) throws Exception {
        Map<String, Object> map = new HashMap<>();
        productionPlan.setLimit(productionPlan.getLimit() == null ? 10 : productionPlan.getLimit());
        productionPlan.setOffset(productionPlan.getOffset() == null ? 0 : productionPlan.getOffset());
        //设置过滤条件
        productionPlan.setFilterEmployeeId(employeeVo.getEmployeeId());
        productionPlan.setFilterOrgId(employeeVo.getOrgId());
        //查询
        map.put("total", productionPlanMapper.queryCount(productionPlan));
        List<ProductionPlanVo> list = productionPlanMapper.queryList(productionPlan);
        map.put("rows", list);
        return map;
    }

    /**
     * 根据生产计划ID查询生产计划信息
     *
     * @param planId 生产计划ID
     * @return
     * @throws Exception
     */
    @Override
    public ProductionPlanVo findProductionPlanById(String planId) throws Exception {
        // 根据生产计划ID查询生产计划信息并返回信息
        return productionPlanMapper.selectByPrimaryKeyNew(planId);
    }

    /**
     * 查询所有生产计划
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<ProductionPlan> queryAll() throws Exception {
        // 查询所有生产计划信息并返回信息
        return productionPlanMapper.queryAll();
    }

    /**
     * 保存
     * 注意：解开启事务，不能抛异常
     *
     *
     * @param employeeVo
     * @param productionPlan 产计划信息
     * @param uploadFiles    上传附件
     * @param deletedFiles   删除附件
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int save(EmployeeVo employeeVo, ProductionPlan productionPlan, String uploadFiles, String deletedFiles) {
        // 判断生产计划信息是否为空
        if (productionPlan == null) {
            return 0;
        }
        // 设置生产计划ID
        productionPlan.setPlanId(UniqueUtil.uuid());
        // 设置生产计划创建人Id
        productionPlan.setCreateUserId(employeeVo.getEmployeeId());
        // 设置生产计划创建时间
        productionPlan.setCreateTime(new Date());
        // 设置生产计划修改人Id
        productionPlan.setModifyUserId(employeeVo.getEmployeeId());
        // 设置生产计划修改时间
        productionPlan.setModifyTime(new Date());
        // 设置删除状态
        productionPlan.setIsDelete((short) 0);
        // 新增信息
        int a = productionPlanMapper.insertSelective(productionPlan);
        if (StringUtils.isNotBlank(uploadFiles)) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            //重新设置附件属性
            for (int i = 0; i < listFiles.size(); i++) {
                File _file = listFiles.get(i);
                //设置主键
                _file.setFileId(UniqueUtil.uuid());
                //设置业务数据id
                _file.setBusinessId(productionPlan.getPlanId());
                _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
            }
            //必须判断非空
            if (listFiles != null && listFiles.size() > 0) {
                a += tdmFileMapper.insertByList(listFiles);
            }
        }
        //需要删除的附件信息
        if (deletedFiles != null && !deletedFiles.equals("")) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(deletedFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    tdmFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return a;
    }

    /**
     * 更新
     *
     * @param employeeVo
     * @param productionPlan
     * @param deletedFiles
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int update(EmployeeVo employeeVo, ProductionPlan productionPlan, String uploadFiles, String deletedFiles) {
        if (productionPlan == null) {
            return 0;
        }
        productionPlan.setModifyUserId(employeeVo.getEmployeeId());
        productionPlan.setModifyTime(new Date());
        int a = productionPlanMapper.updateByPrimaryKeySelective(productionPlan);

        //更新附件信息
        if (StringUtils.isNotBlank(uploadFiles)) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                //重新设置附件属性
                String pId = productionPlan.getPlanId();
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
                    //设置业务数据id
                    _file.setBusinessId(pId);
                }
                a += tdmFileMapper.insertByList(listFiles);
            }
        }
        //需要删除的附件信息
        if (deletedFiles != null && !deletedFiles.equals("")) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(deletedFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    tdmFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return a;
    }

    /**
     * 删除:软删除
     *
     * @param list 生产计划集合
     * @return
     */
    @Override
    public int delByIds(List<String> list) throws Exception {
        // 判断生产计划集合是否为空
        if (list == null || (list != null && list.size() == 0)) {
            return 0;
        }
        // 修改生产计划信息
        int i = productionPlanMapper.updateStateByIds(list);

        return i;
    }
}
